// pages/search/search.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: 1,
    lenovo: [],
    bar: [{
        id: 0,
        name: '人气推荐'
      },
      {
        id: 1,
        name: '销量'
      },
      {
        id: 2,
        name: '价格'
      },
      {
        id: 3,
        name: '新品'
      }
    ],
    rank: true,
    goods: [],
    new: [],
    hot: [],
    currentTab: 0,
    inputvalue: '',
    searchvalue:'',
    level:''
  },
  //关键词搜索加载
  loadsearch() {
    let that = this
    wx.request({
      url: app.globalData.api + "getlinksearch",
      data: {
        search: that.data.searchvalue
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 1) {
          that.setData({
            lenovo: []
          })
        } else {
          that.setData({
            lenovo: res.data.list
          })
        }
      },
    })
    that.setData({
      show: 2
    })
  },

  // 输入触发的事件
  write: function(e) {
    let that = this
    let search = e.detail.value
    console.log(that.data.inputvalue)
    this.setData({
      searchvalue: e.detail.value,
      inputvalue: e.detail.value
    })
    this.loadsearch()
  },
  //点击关键词搜索
  pushsearch: function(e) {
    let that = this
    let search = e.currentTarget.dataset.id;
    this.setData({
      searchvalue: e.currentTarget.dataset.id,
      inputvalue: e.detail.value
    })
    this.loadsearch()
  },
  // 筛选
  swicthbar: function(e) {
    var that = this;
    let currentTab = e.currentTarget.dataset.idx;
    that.setData({
      currentTab: currentTab
    })
    if (currentTab == 0) {
      wx.request({
        url: app.globalData.api + "getsearch",
        data: {
          search: that.data.inputvalue,
          level: that.data.level,
          hits: 'hits'
        },
        success: function(res) {
          console.log(res.data.list)
          if (res.data.code == 1) {
            that.setData({
              goods: []
            })
          } else {
            that.setData({
              goods: res.data.list
            })
          }
        },
      })
    } else if (currentTab == 1) {
      wx.request({
        url: app.globalData.api + "getsearch",
        data: {
          search: that.data.inputvalue,
          level: that.data.level,
          xnum: 'xnum'
        },
        success: function(res) {
          console.log(res.data.list)
          if (res.data.code == 1) {
            that.setData({
              goods: []
            })
          } else {
            that.setData({
              goods: res.data.list
            })
          }
        },
      })
    } else if (currentTab == 2) {
      that.setData({
        rank: !that.data.rank
      })
      wx.request({
        url: app.globalData.api + "getsearch",
        data: {
          search: that.data.inputvalue,
          level: that.data.level,
          price: that.data.rank
        },
        success: function(res) {
          if (res.data.code == 1) {
            that.setData({
              goods: []
            })
          } else {
            that.setData({
              goods: res.data.list
            })
          }
        },
      })
    } else if (currentTab == 3) {
      wx.request({
        url: app.globalData.api + "getsearch",
        data: {
          search: that.data.inputvalue,
          level: that.data.level,
          createtime: 'createtime'
        },
        success: function(res) {
          console.log(res.data.list)
          if (res.data.code == 1) {
            that.setData({
              goods: []
            })
          } else {
            that.setData({
              goods: res.data.list
            })
          }
        },
      })
    }
  },
  //确认搜索
  confirm: function(e) {
    let that = this
    that.setData({
      inputvalue: e.currentTarget.dataset.text
    })
    wx.request({
      url: app.globalData.api + "getsearch",
      data: {
        search: e.currentTarget.dataset.text,
        level: that.data.level,
        hits: 'hits'
      },
      success: function(res) {
        console.log(res.data.list)
        if (res.data.code == 1) {
          that.setData({
            goods: []
          })
        } else {
          that.setData({
            goods: res.data.list
          })
        }
      },
    })
    that.setData({
      show: 3
    })
  },
  //跳详情页
  jumpdetail: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function(options) {
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        console.log(res.data.id)
        that.setData({
          level: res.data.level,
        })
      },
    })
    // 最近搜索
    wx.request({
      url: app.globalData.api + "getnewsearch",
      success: function(res) {
        that.setData({
          new: res.data.list
        })
      },
    })
    //热门搜索
    wx.request({
      url: app.globalData.api + "gethitssearch",
      success: function(res) {
        console.log(res)
        that.setData({
          hot: res.data.list
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})